//
//  DHSizeScrollView.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/1/15.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHSizeScrollView.h"

@interface DHSizeScrollView()

@end

@implementation DHSizeScrollView

- (NSArray *)sizesArr{
    if (!_sizesArr) {
        _sizesArr = [NSArray array];
    }
    return _sizesArr;
}

- (void)loadSizesData:(NSArray *)sizesArr{
    self.sizesArr = sizesArr;
    [self removeAllSubviews];
    CGFloat spaceX = 3;
    CGFloat W = 41;
    [sizesArr enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *itemButton = [[UIButton alloc] initWithFrame:CGRectMake((spaceX + W) * idx + spaceX, 8, W, W)];
        [itemButton setTitle:[obj objectForKey:@"size"] forState:UIControlStateNormal];
        [itemButton setTitleColor:[UIColor colorWithHexString:@"#4A4A4A"] forState:UIControlStateNormal];
        itemButton.titleLabel.font = [UIFont systemFontOfSize:16];
        itemButton.layer.cornerRadius = W / 2;
        itemButton.tag = idx;
        [itemButton addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
        if ([[obj objectForKey:@"inventory"] integerValue] - [[obj objectForKey:@"locked"] integerValue] <= 0) {
            itemButton.userInteractionEnabled = NO;
            [itemButton setTitleColor:RGB(200, 200, 200) forState:UIControlStateNormal];
        }
        [self addSubview:itemButton];
    }];
    
    self.contentSize = CGSizeMake(W * sizesArr.count + spaceX * (sizesArr.count + 1), self.height);
}

- (void)clickAction:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.isSelected) {
        UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(4.5, 4.5, 32, 32)];
        bgImageView.tag = 1000;
        bgImageView.image = [UIImage imageNamed:@"size_bg"];
        bgImageView.layer.cornerRadius = 32/2;
        bgImageView.layer.masksToBounds = YES;
        bgImageView.alpha = 0;
        [sender insertSubview:bgImageView atIndex:0];
        [sender setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [UIView animateWithDuration:0.2 animations:^{
            bgImageView.alpha = 1.0;
        }];
    }else{
        for (UIView *view in sender.subviews) {
            if (view.tag == 1000) {
                [view removeFromSuperview];
            }
        }
        [sender setTitleColor:[UIColor colorWithHexString:@"#4A4A4A"] forState:UIControlStateNormal];
    }
    
    for (UIButton *item in self.subviews) {
        if (item.tag != sender.tag) {
            item.selected = NO;
            for (UIView *view in item.subviews) {
                if (view.tag == 1000) {
                    [view removeFromSuperview];
                }
            }
            if (item.userInteractionEnabled) {
                [item setTitleColor:[UIColor colorWithHexString:@"#4A4A4A"] forState:UIControlStateNormal];
            }
        }
    }
    
    if (sender.isSelected) {
        NSDictionary *dic = [self.sizesArr objectAtIndex:sender.tag];
        [self.delegate sizeScrollViewSelected:[dic objectForKey:@"_id"]];
    }else{
        [self.delegate sizeScrollViewSelected:nil];
    }
}

@end
